﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GAMS.DAL;
using KPIS.GERP.GAMS.MODEL;
using System.Transactions;
using System.Collections.Generic;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ACM_GeneralLedger : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        D_ACM_GENERATE_ACCOUNT_Auto auto = new D_ACM_GENERATE_ACCOUNT_Auto();
        D_ACM_MAS_BaseAccount bAcc = new D_ACM_MAS_BaseAccount();
        D_ACM_GENERAL_Ledger gl = new D_ACM_GENERAL_Ledger();
        D_BGM_MAS_Source source = new D_BGM_MAS_Source();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDDLBgmSourceSeq();
                txtBgmYear.Text = source.GetCurrentBgmYear();
                //LoadGridMaster();
            }
        }

        protected void grdACMAccountAuto_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGridMaster();
            }
        }

        protected void LoadGridMaster()
        {
            Nullable<int> bgm_year = main.ConvertNullInt(txtBgmYear.Text);
            Nullable<int> bgm_source_seq = main.ConvertNullInt(ddlBgmSourceSeq.SelectedValue);
            string tran_code = "";
            string tran_type = rblTranType.SelectedValue;

            var list = gl.GetHeaderFullData(bgm_year, bgm_source_seq, null, tran_type, tran_code, dpDateStart.GetDateValue(), dpDateEnd.GetDateValue());

            grdACMAccountAuto.DataSource = list;
            grdACMAccountAuto.DataBind();
        }

        protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
        {
            GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
            string strTranSeq = main.WebString(dataItem["tran_seq"].Text);
            e.DetailTableView.DataSource = gl.GetDtlsFullData(null, null, null, null, null, null, null, main.ConvertNullInt(strTranSeq));
        }

        protected void LoadDDLBgmSourceSeq()
        {
            ddlBgmSourceSeq.DataSource = source.GetList();
            ddlBgmSourceSeq.DataBind();

            ddlBgmSourceSeq.SelectedIndex = 0;
        }

        protected void cmdFilter_Click(object sender, EventArgs e)
        {
            LoadGridMaster();

            divAddNew.Visible = rblTranType.SelectedValue == "A";
            grdACMAccountAuto.Columns.FindByUniqueName("edit").Visible = rblTranType.SelectedValue == "A";
        }

        protected void cmdPost_Click(object sender, EventArgs e)
        {
            int PostedCount = 0;
            int ErrorCount = 0;
            string strError = "";

            foreach (GridDataItem item in grdACMAccountAuto.Items)
            {
                if (item.Selected)
                {
                    int nTranSeq = Convert.ToInt32(main.WebString(item["tran_seq"].Text));

                    if (gl.CheckDrCr(nTranSeq))
                    {
                        gl.Post(nTranSeq);
                        PostedCount++;
                    }
                    else
                    {
                        ErrorCount++;
                        strError = "และไม่สามารถ Post ได้ [" + ErrorCount.ToString("#,##0") + "] รายการ";
                    }
                }
            }

            lblSummary.Text = "สามารถ Post ได้ [" + PostedCount.ToString("#,##0") + "] รายการ" + strError;
            LoadGridMaster();
        }

        protected void grdACMAccountAuto_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                if (e.Item.OwnerID == "ctl00_ContentPlaceHolder1_grdACMAccountAuto_ctl00")
                {
                    var dataItem = e.Item as GridDataItem;

                    string strStatus = main.WebString(dataItem["record_status"].Text);

                    if (strStatus == "A")
                    {
                        dataItem["ClientSelectColumn"].Enabled = false;
                        dataItem["record_status_name"].ForeColor = System.Drawing.Color.Green;
                    }
                    else
                    {
                        dataItem["ClientSelectColumn"].Enabled = true;
                        dataItem["record_status_name"].ForeColor = System.Drawing.Color.Red;
                    }
                }
            }
        }

        protected void grdACMAccountAuto_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            Nullable<int> bgm_year = main.ConvertNullInt(txtBgmYear.Text);
            Nullable<int> bgm_source_seq = main.ConvertNullInt(ddlBgmSourceSeq.SelectedValue);
            string tran_code = "";
            string tran_type = rblTranType.SelectedValue;

            var list = gl.GetHeaderFullData(bgm_year, bgm_source_seq, null, tran_type, tran_code, dpDateStart.GetDateValue(), dpDateEnd.GetDateValue());

            grdACMAccountAuto.DataSource = list;
        }
    }
}